//
//  FeedbackViewController.m
//  ForestPack
//
//  Created by 郑洲 on 2018/7/4.
//  Copyright © 2018年 郑洲. All rights reserved.
//

#import "FeedbackViewController.h"

//问题类型 1 产品建议 2 功能建议
typedef NS_ENUM(NSInteger, FeedType) {
    FeedType_Goods,
    FeedType_Function
};

@interface FeedbackViewController ()

@property (nonatomic, strong) UIButton    *goodsBtn;
@property (nonatomic, strong) UIButton    *functionBtn;
@property (nonatomic, strong) UITextView  *textView;
@property (nonatomic, strong) UITextField *connectField;
@property (nonatomic,assign)  FeedType     feedType;


@end

@implementation FeedbackViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"问题反馈";
    
    [self setUI];
    
    self.feedType = FeedType_Goods;
}

-(void)sendInfoClick
{
    if (self.textView.text.length <= 0) {
        [HttpManager showNoteMsg:@"请输入您的建议后再提交"];
        return;
    }
    [self requestData];
}

-(void)requestData
{
    if (self.textView.text.length == 0) {
        [HttpManager showNoteMsg:@"请填写相关联系方式"];
        return;
    }
    [self.view endEditing:YES];
    NSString *url = [NSString stringWithFormat:@"%@app/problem_insert",REQUESTHEADER];
    NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithDictionary:@{@"token":[FPLoginAndRegisterTools getToken],@"type":@(self.feedType),@"content":self.textView.text}];
    if (self.connectField.text.length > 0) {
        if ([self.connectField.text containsString:@"@"]) {
            [dict setObject:self.connectField.text forKey:@"email"];
        }else {
            [dict setObject:self.connectField.text forKey:@"phone"];
        }
    }
    
    [HttpManager postHttpRequestByPost:url andParameter:dict success:^(id successResponse) {
        NSString *status = [NSString stringWithFormat:@"%@",successResponse[@"status"]];
        if ([status isEqualToString:@"1"]) {
            [HttpManager showNoteMsg:@"反馈成功，我们将尽快完善"];
            dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC));
            dispatch_after(delayTime, dispatch_get_main_queue(), ^{
                [self.navigationController popViewControllerAnimated:true];
            });
            
        }else{
            [HttpManager showNoteMsg:[NSString stringWithFormat:@"%@",successResponse[@"msg"]]];
        }
    } andFailure:^(id failureResponse) {
        [HttpManager showFail];
    }];
}

- (void)setUI {
    UIView *note_line = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenX375(15), ScreenX375(3), ScreenX375(23))];
    note_line.backgroundColor = ThemeColor;
    [self.view addSubview:note_line];
    
    UILabel *noteLabel = [[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(13), ScreenX375(15), ScreenX375(150), ScreenX375(24))];
    noteLabel.text = @"选择反馈类型";
    noteLabel.textColor = REXADECIMALCOLOR(@"#333333");
    noteLabel.font = Kfont(18);
    [self.view addSubview:noteLabel];
    
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenX375(53), SCREEN_WIDTH, 1)];
    line.backgroundColor = LineColor;
    [self.view addSubview:line];
    
    self.goodsBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, ScreenX375(60), ScreenX375(113), ScreenX375(34))];
    [self.goodsBtn setTitle:@"  产品建议" forState:UIControlStateNormal];
    [self.goodsBtn setTitleColor:REXADECIMALCOLOR(@"#333333") forState:UIControlStateNormal];
    self.goodsBtn.titleLabel.font = Kfont(16);
    [self.goodsBtn setImage:[UIImage imageNamed:@"未选中"] forState:UIControlStateNormal];
    [self.goodsBtn setImage:[UIImage imageNamed:@"选中"] forState:UIControlStateSelected];
    self.goodsBtn.selected = YES;
    [self.goodsBtn addTarget:self action:@selector(selectType:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.goodsBtn];
    
    self.functionBtn = [[UIButton alloc] initWithFrame:CGRectMake(ScreenX375(142), ScreenX375(60), ScreenX375(113), ScreenX375(34))];
    [self.functionBtn setTitle:@"  功能建议" forState:UIControlStateNormal];
    [self.functionBtn setTitleColor:REXADECIMALCOLOR(@"#333333") forState:UIControlStateNormal];
    self.functionBtn.titleLabel.font = Kfont(16);
    [self.functionBtn setImage:[UIImage imageNamed:@"未选中"] forState:UIControlStateNormal];
    [self.functionBtn setImage:[UIImage imageNamed:@"选中"] forState:UIControlStateSelected];
    self.functionBtn.selected = NO;
    [self.functionBtn addTarget:self action:@selector(selectType:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.functionBtn];
    
    _textView = [[UITextView alloc] initWithFrame:CGRectMake(ScreenX375(13), ScreenX375(100), SCREEN_WIDTH - ScreenX375(26), ScreenX375(112))];
    _textView.layer.cornerRadius = ScreenX375(5);
    _textView.layer.borderColor = LineColor.CGColor;
    _textView.layer.borderWidth = 1;
    _textView.clipsToBounds = YES;
    [self.view addSubview:_textView];
    
    _connectField = [[UITextField alloc] initWithFrame:CGRectMake(ScreenX375(13), ScreenX375(270), SCREEN_WIDTH - ScreenX375(26), ScreenX375(44))];
    _connectField.placeholder = @"填写您的联系电话或者邮箱";
    _connectField.font = Kfont(14);
    _connectField.layer.cornerRadius = ScreenX375(22);
    _connectField.backgroundColor = TABLEVIEW_BACKGROUNDCOLOR;
    _connectField.clipsToBounds = YES;
    _connectField.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:_connectField];
    
    OperateButton *style = [[OperateButton alloc] initWithFrame:CGRectMake(ScreenX375(13), ScreenX375(330), ScreenX375(349), ScreenX375(44))];
    [style setTitle:@"提交" forState:UIControlStateNormal];
    [style addTarget:self action:@selector(sendInfoClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:style];
    
    UILabel *phoneLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT - NAVIGATION_BAR_HEIGHT - ScreenX375(56), SCREEN_WIDTH, ScreenX375(56))];
    phoneLabel.text = @"客服电话：0571-8868880";
    phoneLabel.textColor = ThemeColor;
    phoneLabel.textAlignment = NSTextAlignmentCenter;
    phoneLabel.font = Kfont(12);
    [self.view addSubview:phoneLabel];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

- (void)selectType:(UIButton *)button {
    if (button == _goodsBtn) {
        self.feedType = FeedType_Goods;
        _goodsBtn.selected = YES;
        _functionBtn.selected = NO;
    }else {
        self.feedType = FeedType_Function;
        _goodsBtn.selected = NO;
        _functionBtn.selected = YES;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
